package com.sxfq.loansupermarket.backsystem.modular.marketing.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.common.exception.BizExceptionEnum;
import com.sxfq.loansupermarket.backsystem.common.exception.BussinessException;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.core.util.DateUtil;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.core.util.ToolUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.marketing.entity.ReservationsInfo;
import com.sxfq.loansupermarket.backsystem.modular.marketing.entity.SearchReservationsInfo;
import com.sxfq.loansupermarket.backsystem.modular.marketing.entity.TongJiInfo;
import com.sxfq.loansupermarket.backsystem.modular.marketing.service.IReservationsService;
import com.sxfq.loansupermarket.backsystem.modular.system.transfer.UserDto;
import com.sxfq.loansupermarket.backsystem.modular.tcCenter.entity.AddVisitedInfo;
import com.sxfq.loansupermarket.backsystem.modular.tcCenter.service.IConsultantService;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @author hzp
 * @Date
 */
@Controller
@RequestMapping("/reservationsNotArrive")
public class ReservationsNotArrivedController extends BaseController {

    @Autowired
    IReservationsService reservationsService;

    private String PREFIX = "/marketing/reservations/";

    @Autowired
    private SqlMapper sqlMapper;

    @Autowired
    IConsultantService service;



    /**
     * 未到点击姓名跳转客户管理详情页面
     */
    @RequestMapping(value = "/to_reservationsNotDetail/{id}")
    public String to_reservationsNotDetail(@PathVariable String id, Model model) {
        if (ToolUtil.isEmpty(id)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        List<AddVisitedInfo> revisitRecordList = service.getRevisitRecordList(id);
        String sql = "select * from reservations_info where id='" + id + "' limit 0,1";
        model.addAttribute("reservationsId", id);
        model.addAttribute("revisitRecordList", revisitRecordList);
        ReservationsInfo reservationsInfo = sqlMapper.selectOne(sql, ReservationsInfo.class);
        if (reservationsInfo != null) {
            if(reservationsInfo.getCustomerSex()!= null){
                if (reservationsInfo.getCustomerSex() == 1) {
                    reservationsInfo.setSex("男");
                } else {
                    reservationsInfo.setSex("女");
                }
            }
        }
        model.addAttribute("reservationsInfo", reservationsInfo);
        model.addAttribute("userName", ShiroKit.getUser().getAccount());
        return PREFIX + "reservationsNotDetail.html";
    }

    /**
     * 未到点击姓名跳转客户管理详情页面
     */
    @RequestMapping(value = "/to_reservationsNotDetails/{id}")
    public String to_reservationsNotDetails(@PathVariable String id, Model model) {
        if (ToolUtil.isEmpty(id)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        List<AddVisitedInfo> revisitRecordList = service.getRevisitRecordList(id);
        String sql = "select * from reservations_info where id='" + id + "' limit 0,1";
        model.addAttribute("reservationsId", id);
        model.addAttribute("revisitRecordList", revisitRecordList);
        ReservationsInfo reservationsInfo = sqlMapper.selectOne(sql, ReservationsInfo.class);
        if (reservationsInfo != null) {
            if(reservationsInfo.getCustomerSex()!= null){
                if (reservationsInfo.getCustomerSex() == 1) {
                    reservationsInfo.setSex("男");
                } else {
                    reservationsInfo.setSex("女");
                }
            }
        }
        model.addAttribute("reservationsInfo", reservationsInfo);
        model.addAttribute("userName", ShiroKit.getUser().getAccount());
        return PREFIX + "reservationsNotDetails.html";
    }

}